package tq.com.tqteacher.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;

import tq.com.systools.system.StringHelper;
import tq.com.tqteacher.R;

public class TQHeader4ViewHold extends FrameLayout {
    private boolean showBack = false;
    private String title;
    private int resRight = 0;

    private ImageView backView;
    private TextView titleView;
    private FrameLayout rightLayout;

    public TQHeader4ViewHold(Context context){
        this(context, null);
    }

    public TQHeader4ViewHold(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }
    protected void initView(AttributeSet pAttributeSet){
        LayoutInflater.from(getContext()).inflate(R.layout.view_header_4_layout, this, true);

        TypedArray typedArray = getContext().obtainStyledAttributes(pAttributeSet, R.styleable.TQHeader4ViewHold);
        showBack = typedArray.getBoolean(R.styleable.TQHeader4ViewHold_TQHeader4ViewHold_showBack, false);
        title = typedArray.getString(R.styleable.TQHeader4ViewHold_TQHeader4ViewHold_title);
        resRight = typedArray.getResourceId(R.styleable.TQHeader4ViewHold_TQHeader4ViewHold_right, 0);
        typedArray.recycle();


        backView = findViewById(R.id.back);
        titleView = findViewById(R.id.title);
        rightLayout = findViewById(R.id.right);

        backView.setVisibility(showBack ? VISIBLE : INVISIBLE);
        if(!StringHelper.isEmpty(title)){
            titleView.setText(title);
        }
        if(resRight != 0){
            LayoutInflater.from(getContext()).inflate(resRight, rightLayout, true);
        }

        setSpacerHight();
    }

    protected void setSpacerHight(){
        View h = findViewById(R.id.spacer);
        ConstraintLayout.LayoutParams lLayoutParams = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusHeight());
        h.setLayoutParams(lLayoutParams);
    }
    protected int statusHeight(){
        int statusBarHeight1 = -1;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight1;
    }

    public void setBackClickListener(OnClickListener pOnClickListener){
        backView.setOnClickListener(pOnClickListener);
    }
}
